package com.aaa.aspect;

import cn.hutool.core.util.ReflectUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/2 9:13
 * @description：修改，插入切面类
 * @modified By：
 * @version: 1.0
 */
@Component
@Aspect
@EnableAspectJAutoProxy
public class AddUpdateAspect {
    @Pointcut("@annotation(com.aaa.aspect.InsertAnn)")
    public  void insert(){}
    @Pointcut("@annotation(com.aaa.aspect.UpdateAnn)")
    public  void update(){}

    @Before("insert()")
    public  void before(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(args!=null && args.length>=1){
            //保存方法
            ReflectUtil.invoke(args[0],"setCreateTime",new Date());
            try{
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                if(requestAttributes != null){
                    HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
                    Map<String, Object> userInfo= (Map<String, Object>) session.getAttribute("userInfo");
                    //测试者登录会进入日志,但是数据库没有createBy列,会报错
                    ReflectUtil.invoke(args[0],"setCreateBy", userInfo.get("username"));
                }

            }catch (Exception e){
                System.out.println(name+"方法对应的对应实体类没有createBy这个属性!");
            }

        }

    }
    @Before("update()")
    public  void beforeUpdate(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(args!=null && args.length>=1){
            //保存方法
            ReflectUtil.invoke(args[0],"setUpdateTime",new Date());
            try{
                //测试者登录会进入日志,但是数据库没有createBy列,会报错
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                if(requestAttributes != null){
                    HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
                    Map<String, Object> userInfo= (Map<String, Object>) session.getAttribute("userInfo");
                    //测试者登录会进入日志,但是数据库没有createBy列,会报错
                    ReflectUtil.invoke(args[0],"setUpdateBy", userInfo.get("username"));
                }

            }catch (Exception e){
                System.out.println(name+"方法对应的对应实体类没有updateBy这个属性!");
            }

        }

    }

}
